//
//  MapViewController.swift
//  FoodPin
//
//  Created by 王巍 on 16/5/9.
//  Copyright © 2016年 ww. All rights reserved.
//

import UIKit

import MapKit

class MapViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet var mapView: MKMapView!
    
    var restaurant: Restaurant!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 地图上定制显示各种信息
        mapView.showsTraffic = true
        mapView.showsUserLocation = true
        mapView.showsCompass = true
        mapView.showsScale = true
        mapView.showsBuildings = true
        
        // 地址转换
        let geocoder = CLGeocoder()
        geocoder.geocodeAddressString(restaurant.location) { (placemarks, error) -> Void in
            // 失败，打印错误信息
            if error != nil{
                print(error)
                return
            }
            
            if let placemarks = placemarks{
                // 取第一项
                let placemark = placemarks[0]
                
                // 设置标注
                let annotation = MKPointAnnotation()
                
                annotation.title = self.restaurant.name
                annotation.subtitle = self.restaurant.type
                
                if let location = placemark.location{
                    // 标注的数据坐标
                    annotation.coordinate = location.coordinate
                    // 显示多个标注
                    self.mapView.showAnnotations([annotation], animated: true)
                    // 选中标注
                    self.mapView.selectAnnotation(annotation, animated: true)
                }
                
            }
            
        }
        

    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    // 自定义图钉左边显示图片
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let id = "myAnnotation"
        // 是否为用户位置（用户位置也是一个annotation）
        if annotation is MKUserLocation{
            return nil
        }
        var annotationView = self.mapView.dequeueReusableAnnotationViewWithIdentifier(id) as? MKPinAnnotationView
        if annotationView == nil{
            annotationView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: id)
            annotationView?.canShowCallout = true
        }
        
        let imageView = UIImageView(frame: CGRectMake(0, 0, 53, 53))
        imageView.image = UIImage(data: restaurant.image!)
        
        // 设置左边的部件为自定义图片
        annotationView?.leftCalloutAccessoryView = imageView
        annotationView?.pinTintColor = UIColor.greenColor()
        return annotationView
        
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
